<?php
/**
 * Created by PhpStorm.
 * User: 聂尧
 * function：后台课程管理中心
 * Date: 2017/8/23
 * Time: 13:51
 */

namespace app\admin\controller;
use       think\Controller;
use       think\Db;
use       think\Request;

class Room extends Controller
{
    //初始化函数
    public function _initialize()
    {

    }
    //课程列表
    public function room_list()
    {
    	
    	$count=Db::table('x_room')->count();
    	$list =Db::table('x_room')->select();
    	$this->assign('list',$list);
    	$this->assign('count',$count);
    	return $this->fetch();
    }
    //审核通过
    public function examine_pass()
    {
        $room_id =Request::instance()->post('room_id');
        $Data   = array('is_good'=>1);
        //更新状态
        $Offest =Db::table('x_room')->where('room_id=:room_id',['room_id'=>$room_id])->update($Data);
        //创建房间
        //返回操作数据库状态
        if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    }
    //审核拒绝
    public function examine_stop()
    {
        $room_id =Request::instance()->post('room_id');
        //更新状态
		$Data   = array('is_good'=>-1);
        $Offest =Db::table('x_room')->where('room_id=:room_id',['room_id'=>$room_id])->update($Data);
        //返回操作数据库状态
        if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    }
    //课程分类列表
    public function type_list()
    {
        $count=Db::table('x_room_stype')->count();
        $list =Db::table('x_room_stype')->select();
        $this->assign('count',$count);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加课程分类
    public function type_add()
    {
    	 if(Request::instance()->isAjax())
    	 {
               $data   =Request::instance()->post();
               $Offest =Db::table('x_room_stype')->insert($data);
               if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    	 }
    	 else
    	 {
    		return $this->fetch();
    	 }
    }
    //更新课程分类
    public function type_update()
    {
        if(Request::instance()->isAjax())
        {
             $data=Request::instance()->post();
             $name=array('name'=>$data['name']);
             $Offest=Db::table('x_room_stype')->where('id',$data['id'])->update($name);
             if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}        
        }
        else
        {
            $id=Request::instance()->get('id');
            $this->assign('id',$id);
            return $this->fetch();
        }
    }

    //分类轮播图列表
    public function piclun_list()
    {
        $type_id=Request::instance()->get('pid');
        $list   =Db::table('x_roomstype_pic')->where('type_id',$type_id)->select();
        $this->assign('id',$type_id);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加轮播图
    public function piclun_add()
    {
        if(Request::instance()->isAjax())
        {
            $data   =Request::instance()->post();
            if($data['jump_url'])
            {
                $data['jump_url']="http://mg.bonayiyou.com/index/home/class_pay.html?room_id=".$data['jump_url'];
            }
            $Offest =Db::table('x_roomstype_pic')->insert($data);
            if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
        }
        else{
            $type_id=Request::instance()->get('type_id');
            $this->assign('type_id',$type_id);
            return $this->fetch();
        }
    }
    //更新轮播图
    public function piclun_update()
    {
        if(Request::instance()->isAjax())
        {
            $data   =Request::instance()->post();
            $NewData=array(
                'pic_url'=>$data['pic_url'],
                'jump_url'=>$data['jump_url'],
                'sort'=>$data['sort']
                );
            //删除替换的图片避免服务器内存浪费
            // unlink(".".$data['oldpic']);
             $Offest =Db::table('x_roomstype_pic')->where('id',$data['id'])->update($NewData);
             if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
        }
       else{
            $id=Request::instance()->get('id');
            $list=Db::table('x_roomstype_pic')->where('id',$id)->find();
            $this->assign('id',$id);
            $this->assign('list',$list);
            return $this->fetch();
          }
    }
    
    //删除一条轮播图
    public function piclun_del()
    {
        $id     =Request::instance()->get('id');
        //$list   =Db::table('x_roomstype_pic')->where('id',$id)->find();
        //删除服务器照片 清楚多余不要的内存
        $Offest=Db::table('x_roomstype_pic')->where('id',$id)->delete();
        if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    }

    //删除一个分类
    public function delete_type()
    {
        $id=Request::instance()->get('id');
        $Offest=Db::table('x_room_stype')->where('id',$id)->delete();
        if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    }

    //删除一条课程记录
    public function delete_room()
    {
        $id=Request::instance()->get('id');
        $Offest=Db::table('x_room')->where('room_id',$id)->delete();
        if($Offest){return json(['code'=>1]);}else{return json(['code'=>0]);}
    }
  
}

?>